package controller

import (
	"heartbeat/utils"
	"strings"

	"fmt"

	"heartbeat/database"

	"time"

	"log"

	"gopkg.in/kataras/iris.v6"
)

type Heartbeat struct{}

func (ctrl *Heartbeat) Delete(ctx *iris.Context) {
	id := strings.TrimSpace(ctx.Param("id"))
	if id == "" || id == "*" {
		ctx.SetStatusCode(403)
		return
	}
	redis := database.GetRedisClient()
	config := utils.GetConfig().Other
	if err := redis.Del(fmt.Sprintf(config["redisprefix"], id)).Err(); err != nil {
		ctx.SetStatusCode(500)
		return
	}
	ctx.SetStatusCode(200)
}
func (ctrl *Heartbeat) Get(ctx *iris.Context) {
	fmt.Println(1)
	mac := ctx.URLParam("mac")
	mac = strings.TrimSpace(mac)
	if mac == "" {
		ctx.SetStatusCode(201)
		ctx.Writef("mac 为空")
		return
	}
	md5 := utils.GetMD5(mac)
	config := utils.GetConfig().Other
	redisKey := fmt.Sprintf(config["redisprefix"], md5)
	redis := database.GetRedisClient()
	if err := redis.HSet(redisKey, "ip", mac).Err(); err != nil {
		log.Fatalln(err)
	}
	if err := redis.HSet(redisKey, "time", time.Now().Unix()).Err(); err != nil {
		log.Fatalln(err)
	}
	ctx.SetStatusCode(200)
	return
}

type StoreValue struct {
	IP   string `json:"ip"`
	Time int64  `json:"time"`
	ID   string `json:"id"`
}

func (ctrl *Heartbeat) GetBadList(ctx *iris.Context) {
	respQueue, err := GetTimeoutVPS()
	if err != nil {
		log.Fatalln(err)
		ctx.SetStatusCode(500)
		return
	}
	ctx.JSON(200, respQueue)
}

var HeartbeatCtr *Heartbeat
